package com.spc.car.data.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.data.annotation.Like;
import com.spc.car.data.annotation.Skip;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@UtilityClass
public class QueryUtil {

    public static QueryWrapper toWrapper(Object obj) {
        QueryWrapper wrapper = new QueryWrapper();

        for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
        {
            try
            {
                Field[] fields = superClass.getDeclaredFields();
                if (ArrayUtils.isNotEmpty(fields)) {
                    for(Field field : fields) {
                        if (field.isAnnotationPresent(Skip.class)) {
                            continue;
                        }
                        field.setAccessible(true);
                        Object value = field.get(obj);
                        if (field.getType().equals(String.class)
                                && field.isAnnotationPresent(Like.class)
                                && null != value) {
                            wrapper.like(StringUtils.isNotBlank((String)value),
                                    underscoreName(field.getName()),
                                    value);
                        }else {
                            wrapper.eq(null != value,
                                    underscoreName(field.getName()), value);
                        }

                    }
                }
            }
            catch (Exception e) {
                continue;
            }
        }
        log.info("condition >> {} : {}", wrapper.getSqlSegment(), wrapper.getTargetSql());
        return wrapper;
    }

    public List<String> selectField(Class clz, String... ignoreField) {
        List<String> fieldNameList = new ArrayList<>();
        List<String> ignoreFieldList = ArrayUtils.isEmpty(ignoreField) ? null : Arrays.asList(ignoreField);
        try {
            Field[] fields = clz.getDeclaredFields();
            if (ArrayUtils.isNotEmpty(fields)) {
                for(Field field : fields) {
                    String underscoreFldName = underscoreName(field.getName());
                    if (!CollectionUtils.isEmpty(ignoreFieldList)
                            && ignoreFieldList.contains(underscoreFldName) ) {
                        continue;
                    }
                    fieldNameList.add(underscoreFldName);
                }
            }
        } catch (Exception e) {

        }
        return fieldNameList;
    }

    public static String underscoreName(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            result.append(camelCaseName.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(Character.toLowerCase(ch));
                } else {
                    result.append(ch);
                }
            }
        }
        return result.toString();
    }
}
